package com.练习案例.多线程抽奖并求取最大值;

import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{//五个人抽三个红包，红包一共一百
    ArrayList<Integer> list=new ArrayList<>();

    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }

    static ArrayList<Integer> list1=new ArrayList<>();
    static ArrayList<Integer> list2=new ArrayList<>();

    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.class){
                if(list.size()==0){
                    if ("1".equals(getName())) {
                        System.out.println("1"+list1);
                    }else{
                        System.out.println("2"+list2);
                    }
                    break;
                }else{
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    if ("1".equals(getName())) {
                        list1.add(prize);
                    }else{
                        list2.add(prize);
                    }        //System.out.println(getName()+"抽到了"+prize);
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
